package com.sy.service.saleService.impl;

import com.sy.bean.sale.ErpSale;
import com.sy.bean.sale.params.AddSale;
import com.sy.bean.sale.params.SaleQuery;
import com.sy.bean.sale.params.UpStatus;
import com.sy.common.bean.PageBean;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.common.utils.BeanOfMap;
import com.sy.mapper.saleMapper.ErpOrderMapper;
import com.sy.mapper.saleMapper.ErpSaleDetailMapper;
import com.sy.mapper.saleMapper.ErpSaleMapper;
import com.sy.service.saleService.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SaleServiceImpl extends BaseServiceImpl<ErpSale, ErpSaleMapper> implements SaleService {
    @Autowired
    ErpSaleMapper erpSaleMapper;
    @Autowired
    ErpSaleDetailMapper erpSaleDetailMapper;
    @Autowired
    ErpOrderMapper erpOrderMapper;

    @Transactional
    @Override
    public int save(AddSale addSale) {
        int rs = erpSaleMapper.save(addSale);
        if (rs < 1)
            throw new RuntimeException("保存失败");
        rs = erpSaleDetailMapper.save(addSale.getId(), addSale);
        if (rs < 1)
            throw new RuntimeException("保存失败");
        rs = erpOrderMapper.updateSId(addSale.getDdId(), addSale.getId());
        if (rs < 1)
            throw new RuntimeException("保存失败");
        return 1;
    }

    @Transactional
    @Override
    public int upStatus(UpStatus upStatus) {
        int rs = erpSaleMapper.upStatus(upStatus);
        if (rs < 1)
            throw new RuntimeException("审核失败");
        rs = erpSaleDetailMapper.upStatus(upStatus);
        if (rs < 1)
            throw new RuntimeException("审核失败");
        return 1;
    }

    @Transactional
    @Override
    public int delete(int id) {
        int rs = erpSaleMapper.delete(id);
        if (rs < 1)
            throw new RuntimeException("删除失败");
        int num = erpSaleDetailMapper.queryBySId(id);
        if (num == 1) {
            rs = erpSaleDetailMapper.deleteBySId(id);
            if (rs < 1)
                throw new RuntimeException("删除失败");
        }
        num = erpOrderMapper.queryBySId(id);
        if (num > 0) {
            rs = erpOrderMapper.deleteBySId(id);
            if (rs < 1)
                throw new RuntimeException("删除失败");
        }
        return 1;
    }
}
